﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace Tum.CollabXT.TFS
{
    class Reports : IProcessor
    {
        public void Process(IProcessProvider processProvider, string outputPath, Log log)
        {
            string reportsTemplateFileName = outputPath + "/Reports/ReportsTasks.xml";

            #region Load template
            XmlDocument reportsDoc = new XmlDocument();
            log.AddEntry("Loading reports template ...");
            try
            {
                reportsDoc.Load(reportsTemplateFileName);
            }
            catch
            {
                log.AddEntryAndThrowException("Could not load classification file at '" + reportsTemplateFileName + "'");
            }
            #endregion

            log.AddEntry("Selecting reports node ...");
            var reportingServicesNode = reportsDoc.SelectSingleNode("/tasks/task/taskXml/ReportingServices");
            if (reportingServicesNode == null)
                log.AddEntryAndThrowException("Not found. Template is not valid");

            string[] reportFiles = Directory.GetFiles(outputPath + "/Reports", "*.rdl");
            if (reportFiles.Length > 0)
            {
                XmlNode reportsNode = reportingServicesNode.AppendChild(reportsDoc.CreateNode("element", "reports", ""));

                foreach (string curReportFile in reportFiles)
                {
                    #region Report node
                    XmlNode newReportNode = reportsDoc.CreateNode("element", "report", "");

                    XmlAttribute nameAttribute = reportsDoc.CreateAttribute("name");
                    string reportFileName = Helper.FileNameFromPath(curReportFile);
                    nameAttribute.Value = reportFileName.Substring(0, reportFileName.Length - 4);
                    newReportNode.Attributes.Append(nameAttribute);

                    XmlAttribute filenameAttribute = reportsDoc.CreateAttribute("filename");
                    filenameAttribute.Value = "Reports/" + curReportFile.Substring(curReportFile.LastIndexOfAny(Helper.FolderSeperators) + 1);
                    newReportNode.Attributes.Append(filenameAttribute);

                    XmlAttribute folderAttribute = reportsDoc.CreateAttribute("folder");
                    folderAttribute.Value = "";
                    newReportNode.Attributes.Append(folderAttribute);

                    XmlAttribute cacheAttribute = reportsDoc.CreateAttribute("cacheExpiration");
                    cacheAttribute.Value = "30";
                    newReportNode.Attributes.Append(cacheAttribute);

                    reportsNode.AppendChild(newReportNode);


                    #region Parameters node
                    XmlNode parametersNode = reportsDoc.CreateNode("element", "parameters", "");
                    XmlNode parameterNode = reportsDoc.CreateNode("element", "parameter", "");

                    XmlAttribute paramNameAttribute = reportsDoc.CreateAttribute("name");
                    paramNameAttribute.Value = "ExplicitProject";
                    parameterNode.Attributes.Append(paramNameAttribute);

                    XmlAttribute paramValAttribute = reportsDoc.CreateAttribute("value");
                    paramValAttribute.Value = "";
                    parameterNode.Attributes.Append(paramValAttribute);

                    parametersNode.AppendChild(parameterNode);
                    newReportNode.AppendChild(parametersNode);
                    #endregion

                    XmlNode dataSourcesNode = reportsDoc.CreateNode("element", "datasources", "");
                    {
                        XmlNode referenceNode = reportsDoc.CreateNode("element", "reference", "");

                        XmlAttribute refNameAttribute = reportsDoc.CreateAttribute("name");
                        refNameAttribute.Value = "/TfsOlapReportDS";
                        referenceNode.Attributes.Append(refNameAttribute);

                        XmlAttribute refDsNameAttribute = reportsDoc.CreateAttribute("dsname");
                        refDsNameAttribute.Value = "TfsOlapReportDS";
                        referenceNode.Attributes.Append(refDsNameAttribute);

                        dataSourcesNode.AppendChild(referenceNode);
                    }
                    {
                        XmlNode referenceNode = reportsDoc.CreateNode("element", "reference", "");

                        XmlAttribute refNameAttribute = reportsDoc.CreateAttribute("name");
                        refNameAttribute.Value = "/TfsReportDS";
                        referenceNode.Attributes.Append(refNameAttribute);

                        XmlAttribute refDsNameAttribute = reportsDoc.CreateAttribute("dsname");
                        refDsNameAttribute.Value = "TfsReportDS";
                        referenceNode.Attributes.Append(refDsNameAttribute);

                        dataSourcesNode.AppendChild(referenceNode);
                    }
                    newReportNode.AppendChild(dataSourcesNode);
                    #endregion
                }
            }

            #region Save classification file
            log.AddEntry("Saving classification file ...");
            try
            {
                reportsDoc.Save(reportsTemplateFileName);
            }
            catch
            {
                log.AddEntryAndThrowException("Saving reports file to '" + reportsTemplateFileName + "' failed");
            }
            #endregion
        }
    }
}
